import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { ApiApi } from '#/api/system/api';

import { useAccess } from '@vben/access';

const { hasAccessByCodes } = useAccess();
export function useApiFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'systemId',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'title',
      label: 'API名称',
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: '请输入API名称',
      },
    },
    {
      fieldName: 'path',
      label: 'API路径',
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: '请输入API路径，如：/api/users',
      },
    },
  ];
}

export function useApiGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'title',
      label: 'API名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入API名称进行搜索',
      },
    },
    {
      fieldName: 'path',
      label: 'API路径',
      component: 'Input',
      componentProps: {
        placeholder: '请输入API路径进行搜索',
      },
    },
  ];
}

/** API列表的字段 */
export function useApiGridColumns<T = ApiApi.Api>(
  onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'seq',
      title: '序号',
      width: 80,
    },
    {
      field: 'title',
      title: 'API名称',
      minWidth: 200,
      showOverflow: 'title',
    },
    {
      field: 'path',
      title: 'API路径',
      minWidth: 300,
      showOverflow: 'title',
      cellRender: {
        name: 'CellCode',
      },
    },
    {
      field: 'editInfo',
      title: '编辑信息',
      slots: { default: 'edit-info' },
      minWidth: 200,
    },
    {
      field: 'operation',
      title: '操作',
      width: 160,
      fixed: 'right',
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'api',
          nameTitle: 'API',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            show: hasAccessByCodes(['system:system:api:edit']),
          },
          {
            code: 'delete',
            show: hasAccessByCodes(['system:system:api:delete']),
          },
        ],
      },
    },
  ];
}
